import java.util.Random;

import jade.content.ContentElement;
import jade.content.lang.Codec.CodecException;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.OntologyException;
import jade.content.onto.basic.Action;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;

public class Agent1 extends Agent implements AgentVocabulary{
//Initiatorul negocierii, asta tre sa trimita parfumul
	
	private MessageOntology ont=(MessageOntology) MessageOntology.getInstance();
	private SLCodec sl=new SLCodec();
	protected void setup(){		
		getContentManager().registerLanguage(sl);
		getContentManager().registerOntology(ont);
		addBehaviour(new OneShotBehaviour() {
			//Initializatorul conversatiei, obligatoriu oneshot
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void action() {
				// TODO Auto-generated method stub
				MessageBody body=new MessageBody();
				body.setAction("INIT");
				body.setContent("Init conversation");
				AID a=new AID("Agent2",AID.ISLOCALNAME);
				ACLMessage msg=new ACLMessage(ACLMessage.INFORM);
				msg.addReceiver(a);
				msg.setLanguage(sl.getName());
				msg.setOntology(ont.getName());
				try {
					getContentManager().fillContent(msg, new Action(a,body));
					send(msg);
				} catch (CodecException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (OntologyException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
		});
		addBehaviour(new CyclicBehaviour() {			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			private Random rnd=new Random();
			@Override			
			public void action() {
				int opt;
				// TODO Auto-generated method stub
				ACLMessage msg = receive();
				if (msg == null) { block(); return; }
				if (msg!=null){					
				try {
					ContentElement cnt=getContentManager().extractContent(msg);
					MessageBody cp=(MessageBody)((Action)cnt).getAction();
					switch (cp.getAction()) {
					case "INIT":
						System.out.println(cp.getContent());						
						break;
					case "CUMPARA":						
						System.out.println("Content trimis: "+cp.getContent());
						if ("Error".equals(cp.getContent())){
							System.out.println("Eroare. Parfum netrimis");
						}
						if ("Deja cumparat".equals(cp.getContent())){
							System.out.println("Eroare. Produs deja cumparat");
						}
						if ("Accepted".equals(cp.getContent())){
							System.out.println("Produs cumparat");							
						}
						break;
					case "NEGOCIERE":
						if ("Error".equals(cp.getContent())){
							System.out.println("Eroare negociere. Parfum netrimis");
						}
						if ("Denied".equals(cp.getContent())){
							System.out.println("Eroare. Negociere respinsa");
						}
						if ("Accepted".equals(cp.getContent())){
							System.out.println("Negociere acceptata. Noul pret este"+cp.getPret());
						}
						break;
					case "SIGKILL":
						//Toate produsele cumparate, termin executia
						System.out.println("Agentul nr 1 a fost oprit");
						doDelete();
						break;
					}
				} catch (CodecException
						| OntologyException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}
				opt=rnd.nextInt(3);//Aleg aleator o actiune intreprinsa
				AID a;
				MessageBody body=new MessageBody();
				switch (opt) {
				case 0:
					//Omul vrea sa cumpere ceva					
					body.setAction("CUMPARA");
					body.setContent(String.valueOf((rnd.nextInt(3))));//aleg aleator un produs pentru a fi cumparat
					a=new AID("Agent2",AID.ISLOCALNAME);
					msg=new ACLMessage(ACLMessage.INFORM);
					msg.addReceiver(a);
					msg.setLanguage(sl.getName());
					msg.setOntology(ont.getName());
					try {
						getContentManager().fillContent(msg, new Action(a,body));
						send(msg);
					} catch (CodecException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (OntologyException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
				case 1:
					//Omul trimite parfumul					
					body.setAction("PARFUM");
					body.setContent("PARFUM");
					a=new AID("Agent2",AID.ISLOCALNAME);
					msg=new ACLMessage(ACLMessage.INFORM);
					msg.addReceiver(a);
					msg.setLanguage(sl.getName());
					msg.setOntology(ont.getName());
					try {
						getContentManager().fillContent(msg, new Action(a,body));
						send(msg);
					} catch (CodecException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (OntologyException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
				case 2:
					//Omul cere micsorarea pretului unui element din lista
					body.setAction("NEGOCIERE");
					body.setContent(String.valueOf(rnd.nextInt(3)));
					body.setPret(rnd.nextInt(20));
					a=new AID("Agent2",AID.ISLOCALNAME);
					msg=new ACLMessage(ACLMessage.INFORM);
					msg.addReceiver(a);
					msg.setLanguage(sl.getName());
					msg.setOntology(ont.getName());
					try {
						getContentManager().fillContent(msg, new Action(a,body));
						send(msg);
					} catch (CodecException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (OntologyException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;				
				}
			}
			
		});
	}
}
